/*
 * memory_utils.c
 *
 *  Created on: Jul 22, 2012
 *      Author: baris
 */
#include "datatypes.h"

/*
// bug in the free_concept* functions!

void free_concept(Concept* c);

void free_role(Role* r) {

}

void free_atomic_concept(AtomicConcept* ac) {
	free(ac->name);
	ac->name = NULL;
	free(ac);
}

void free_conjunction(Conjunction* c) {
	int i;

	for (i = 0; i < c->size; i++) {
		free_concept(c->conjuncts[i]);
		c->conjuncts[i] = NULL;
	}

	free(c->conjuncts);
	c->conjuncts = NULL;
	free(c);
}

void free_existential_restriction(Exists* e) {
	free_role(e->role);
	e->role = NULL;
	free_concept(e->filler);
	e->filler = NULL;
	free(e);
}

void free_concept(Concept* c) {
	int i;

	switch (c->type) {
	case ATOMIC_CONCEPT:
		if (c->description.atomic != NULL) {
			free_atomic_concept(c->description.atomic);
			c->description.atomic = NULL;
		}
		break;
	case CONJUNCTION:
		if (c->description.conj != NULL) {
			free_conjunction(c->description.conj);
			c->description.conj = NULL;
		}
		break;
	case EXISTENTIAL_RESTRICTION:
		if (c->description.exists != NULL) {
			free_existential_restriction(c->description.exists);
			c->description.exists = NULL;
		}
		break;
	default:
		fprintf(stderr,"unknown concept type, aborting\n");
		exit(-1);
	}

	for (i = 0; i < c->told_subsumer_count && c->told_subsumers[i] != NULL; i++) {
		free_concept(c->told_subsumers[i]);
		c->told_subsumers[i] = NULL;
	}

	free(c->told_subsumers);
	c->told_subsumers = NULL;
	free(c);
}


void free_subclass_axiom(SubClassAxiom* ax, char recursive_free_flag) {
	if (recursive_free_flag) {
		// first fix the bug in the free_concept* functions!
		free_concept(ax->lhs);
		ax->lhs = NULL;
		free_concept(ax->rhs);
		ax->rhs = NULL;
	}
	free(ax);
}

void free_subclass_axioms(TBox* tbox, char recursive_free_flag) {
	int i;

	for (i = 0; i < tbox->subclass_axiom_count; i++) {
		free_subclass_axiom(tbox->subclass_axioms[i], recursive_free_flag);
		tbox->subclass_axioms[i] = NULL;
	}

	free(tbox->subclass_axioms);
	tbox->subclass_axioms = NULL;
}
*/

void free_subclass_axioms(TBox* tbox) {
	// int i;

	// for (i = 0; i < tbox->subclass_axiom_count; i++)
	// 	free(tbox->subclass_axioms[i]);

	free(tbox->subclass_axioms);
}
